package es.ats.codington.festival.daos;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementSetter;

import es.ats.codington.festival.domain.Event;
import es.ats.codington.festival.domain.EventSignUp;
import es.ats.codington.festival.domain.Visitor;
import es.ats.codington.festival.interfaces.daos.IEventSignUpDAO;
import es.ats.codington.festival.mappers.EventMapper;
import es.ats.codington.festival.mappers.EventSignUpMapper;
import es.ats.codington.festival.mappers.VisitorMapper;
import es.ats.codington.festival.utils.EventDbQuery;
import es.ats.codington.festival.utils.FERSDataConnection;
import es.ats.codington.festival.utils.JSONQuery;

/**
 * The Class EventSignUpDAOImpl.
 */
public class EventSignUpDAOImpl implements IEventSignUpDAO {
	
	/** The q. */
	int q;
	/** The address. */
	private static String address = "src/es/ats/codington/festival/utils/DBJson.json";
	
	/** The Constant EventSignUp_Register. */
	private static final String EventSignUp_Register = "EventSignUp_Register";
 
	
	/** The jdbc template object. */
	private JdbcTemplate jdbcTemplateObject;
	
	/** The query properties. */
	private Properties queryProperties;

	
	/**
	 * Instantiates a new event sign up dao impl.
	 *
	 * @param jdbcTemplateObject the jdbc template object
	 * @param queryProperties the query properties
	 */
	public EventSignUpDAOImpl(JdbcTemplate jdbcTemplateObject,Properties queryProperties ) {
		this.jdbcTemplateObject = jdbcTemplateObject;
		this.queryProperties = queryProperties;
	} 
	
	/**
	 * Register event by visitor.
	 *
	 * @param idVisitor the id visitor
	 * @param idEvent the id event
	 * @return true, if successful
	 * @throws Exception the exception
	 * @see es.ats.codington.festival.interfaces.daos.IEventSignUpDAO#registerEventByVisitor(java.sql.Connection, int, int)
	 */
	@Override
	public boolean registerEventByVisitor(final int idVisitor, final int idEvent) throws Exception{
		boolean rs = false;
		try{	
			int events =  jdbcTemplateObject.update(queryProperties.getProperty("insert_eventSignUp"),new PreparedStatementSetter(){
				@Override
				public void setValues(PreparedStatement statement)
						throws SQLException {

					statement.setInt(1, idVisitor);
					statement.setInt(2, idEvent);
				}
			});
			if(events>0) rs= true;
		
			
		}catch (Exception e){
			System.out.println(e.getMessage());
		}
		return rs;
	}

	
	/**
	 * Un register event in visitor.
	 *
	 * @param idVisitor the id visitor
	 * @param idEvent the id event
	 * @return true, if successful
	 */
	@Override
	public boolean unRegisterEventInVisitor(final int idVisitor,final int idEvent) {
	boolean rs = false;
	try{	
		int events =  jdbcTemplateObject.update(queryProperties.getProperty("delete_eventSignUp"),new PreparedStatementSetter(){
			@Override
			public void setValues(PreparedStatement statement)
					throws SQLException {

				statement.setInt(1, idVisitor);
				statement.setInt(2, idEvent);
			}
		});
		
		if(events>0) rs= true;
	
		
	}catch (Exception e){
		System.out.println(e.getMessage());
	}
	return rs;
}
	
	
	/**
	 * Gets the events by visitor.
	 *
	 * @param idVisitor the id visitor
	 * @param event the event
	 * @return the events by visitor
	 */
	
	@Override
	public boolean getEventsByVisitor(final int idVisitor, ArrayList<Event> event) {
		boolean rs = false;
		try{	
			
			List<EventSignUp> id_events =  jdbcTemplateObject.query(queryProperties.getProperty("get_num_events_idV"),new PreparedStatementSetter(){
				@Override
				public void setValues(PreparedStatement statement)
						throws SQLException {

					statement.setInt(1, idVisitor);

				}
			}, new EventSignUpMapper());
			
   
		    //get the idEvents
		    final int[] events = new int [id_events.size()]; 
		   
		    
		    
	/*		*/
			
			int j=0;
			for(j=0; j<id_events.size(); j++){
				events[j]=id_events.get(j).getEventId();				
			}
			
		    if (j==0){rs = false;}  
		    else {
		    	//Get the evets
		    	for(q=0; q<events.length; q++){
		    		
		    		List<Event> id_eventos =  jdbcTemplateObject.query(queryProperties.getProperty("get_events_idV"),new PreparedStatementSetter(){
						@Override
						public void setValues(PreparedStatement statement)
								throws SQLException {

							statement.setInt(1, events[q]);

						}
					}, new EventMapper());
		    		event.add(id_eventos.get(0));
		    		  
		    	}
		    	rs = true;
		    	
		    	
		    }
	
		}catch (Exception e){
			System.out.println(e.getMessage());
		}		
		return rs;
	}
	
	
	/**
	 * Gets the semi all events.
	 *
	 * @param idV the id v
	 * @param event2 the event2
	 * @return the semi all events
	 */
		
	@Override
	public boolean getSemiAllEvents(int idV, ArrayList<Event> event2){
			boolean rs = false;
			try{	
				List<Event> id_events =  jdbcTemplateObject.query(queryProperties.getProperty("all_events"),new PreparedStatementSetter(){
					@Override
					public void setValues(PreparedStatement statement)
							throws SQLException {
					}
				}, new EventMapper());

			    //get the idEvents
			    final int[] events = new int [id_events.size()]; 
			    
			    
			    
				int j=0;
				for(j=0; j<id_events.size(); j++){
					events[j]=id_events.get(j).getId();				
				}
				
			    if (j==0){rs = false;}  
			    else {
			    	//Get the evets
			    	for(q=0; q<events.length; q++){
			    		
			    		List<Event> id_eventos =  jdbcTemplateObject.query(queryProperties.getProperty("get_events_idV"),new PreparedStatementSetter(){
							@Override
							public void setValues(PreparedStatement statement)
									throws SQLException {

								statement.setInt(1, events[q]);

							}
						}, new EventMapper());
			    		event2.add(id_eventos.get(0));
	   		
			    		  
			    	}
			    	rs = true;
			    	//meterlos en event2[]
			    }
			    
	    //     System.out.println("Eventos que le faltan a este visitor "+ (events.length));	
				
				
			}catch (Exception e){
				System.out.println(e.getMessage());
			}		
			return rs;
		}

	
	/**
	 * Insert event dao.
	 *
	 * @param con the con
	 * @param event the event
	 * @return true, if successful
	 */
	public boolean insertEventDAO(Connection con, Event event) {
		// TODO Auto-generated method stub
		return false;
	}

}